package com.mall.b2c.domain.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.b2c.bean.CartBean;
import com.mall.b2c.bean.CartItemBean;
import com.mall.b2c.bean.ShopCartItemBean;
import com.mall.b2c.domain.intergral.ShopCartItemDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.common.domain.BaseEntity;

/**
 * 购物车
 */
@Entity
@Table(name = "b2c_cart")
public class CartDomain extends BaseEntity {

	private static final long serialVersionUID = 8578446541779395246L;
	@JsonIgnore private Set<CartItemDomain> cartItemSet;// 购物车货品项
	@JsonIgnore private MemberDomain member;// 会员
	private String cartKey;

	/**
	 * 积分购物车项
	 */
	@JsonIgnore private Set<ShopCartItemDomain> shopCartItems;

	@Transient
	public CartBean getCartInfo(boolean withItem){
		CartBean cart = new CartBean();
		if(cartItemSet != null){
			cart.setItemNum(cartItemSet.size());
			if(withItem){
				List<CartItemBean> list = new ArrayList<CartItemBean>();
				for(CartItemDomain cartItem : cartItemSet){
					list.add(new CartItemBean(cartItem));
				}
				cart.setItems(list);
			}
		}else{
			cart.setItemNum(0);
		}

		if(shopCartItems != null){
//			cart.setItemNum(shopCartItems.size());
			if(withItem){
				List<ShopCartItemBean> list = new ArrayList<>();
				for(ShopCartItemDomain cartItem : shopCartItems){
					list.add(new ShopCartItemBean(cartItem));
				}
				cart.setShopItems(list);
			}
		}else{
//			cart.setItemNum(0);
		}
		return cart;
	}


	@Transient
	public CartBean getShopCartInfo(boolean withItem){
		CartBean cart = new CartBean();
		if(shopCartItems != null){
			cart.setItemNum(shopCartItems.size());
			if(withItem){
				List<ShopCartItemBean> list = new ArrayList<>();
				for(ShopCartItemDomain cartItem : shopCartItems){
					list.add(new ShopCartItemBean(cartItem));
				}
				cart.setShopItems(list);
			}
		}else{
			cart.setItemNum(0);
		}
		return cart;
	}


	@OneToMany(fetch=FetchType.LAZY,mappedBy="cart")
	public Set<CartItemDomain> getCartItemSet() {
		return cartItemSet;
	}

	public void setCartItemSet(Set<CartItemDomain> cartItemSet) {
		this.cartItemSet = cartItemSet;
	}

	@OneToMany(fetch=FetchType.LAZY,mappedBy="cart")
	public Set<ShopCartItemDomain> getShopCartItems() {
		return shopCartItems;
	}

	public void setShopCartItems(Set<ShopCartItemDomain> shopCartItems) {
		this.shopCartItems = shopCartItems;
	}

	@OneToOne(fetch=FetchType.LAZY)
	public MemberDomain getMember() {
		return member;
	}

	public void setMember(MemberDomain member) {
		this.member = member;
	}

	public String getCartKey() {
		return cartKey;
	}

	public void setCartKey(String cartKey) {
		this.cartKey = cartKey;
	}

}
